using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using jQuery.ScriptManager;
using Microsoft.Office.Server;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace JQueryWebParts.TagSuggestion
{
    [Guid("6c282b40-7937-484c-9559-0d4f969e469f")]
    public class SPTagSuggestion : Microsoft.SharePoint.WebPartPages.WebPart
    {
        // UI controls
        protected Literal placeHolderLiteral = new Literal();        
        protected Button searchButton = new Button();
        protected TextBox tagBox = new TextBox();

        /// <summary>
        /// Configurable Properties
        /// </summary>
        protected string searchResultsPage = "/SearchCenter/Pages/results.aspx?k=";
        protected string tagJSONPage = "/_vti_bin/Tags.ashx";

        //JqueryManager .. 
        protected jQueryManager jqueryManager = null;

        protected override void OnInit(EventArgs e)
        {
            //find our JQueryManager, if it doesn't exist add one to the page
            jqueryManager = jQueryManager.GetCurrent(Page);
            if (jqueryManager == null)
            {
                jqueryManager = new jQueryManager();
                Page.Controls.Add(jqueryManager);
            }
            Page.ClientScript.RegisterClientScriptInclude("Tag", Page.ClientScript.GetWebResourceUrl(this.GetType(), "JQueryWebParts.js.tag.js"));
            base.OnInit(e);
        }

        protected override void CreateChildControls()
        {
            searchButton.Click += new EventHandler(searchButton_Click);
            searchButton.Text = "Search";

            this.Controls.Add(placeHolderLiteral);
            this.Controls.Add(tagBox);
            this.Controls.Add(new LiteralControl("<br/><br/>"));
            this.Controls.Add(searchButton);
          

            base.CreateChildControls();
        }

        //Search just does a redirect to the search centre
        void searchButton_Click(object sender, EventArgs e)
        {

            StringBuilder searchString = new StringBuilder();
            //Redirect to search
            string[] tags = tagBox.Text.Split(' ');                       

            if (tags != null)
            {
                foreach (string tag in tags)
                {
                    searchString.Append("tags:");
                    searchString.Append(tag);
                    searchString.Append(" ");                    
                }
            }

            Page.Response.Redirect(searchResultsPage + searchString.ToString());
        }

        
        
        protected override void OnPreRender(EventArgs e)
        {
            //register our JQuery startup function
            tagBox.Attributes.Add("id", tagBox.ClientID);
            jqueryManager.ReadyFunctions.Add(new StartFunction("jQuery('#" + tagBox.ClientID + "').tagSuggest({url: '" + tagJSONPage + "', delay: 250});"));

            base.OnPreRender(e);   
        }

        [System.Web.UI.WebControls.WebParts.WebBrowsable(true),
        System.Web.UI.WebControls.WebParts.WebDisplayName("Search Results Page"),
        System.Web.UI.WebControls.WebParts.WebDescription("URL to the Search Center"),
        System.Web.UI.WebControls.WebParts.Personalizable(
        System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
        System.ComponentModel.Category("Settings"),
        System.ComponentModel.DefaultValue("/SearchCenter/Pages/results.aspx?k=")
       ]
        public string SearchResultsPage 
        {
            get { return searchResultsPage; }
            set { searchResultsPage = value; }
        }

        [System.Web.UI.WebControls.WebParts.WebBrowsable(true),
        System.Web.UI.WebControls.WebParts.WebDisplayName("Tag URL"),
        System.Web.UI.WebControls.WebParts.WebDescription("URL that returns the JSON tags"),
        System.Web.UI.WebControls.WebParts.Personalizable(
        System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
        System.ComponentModel.Category("Settings"),
        System.ComponentModel.DefaultValue("/_vti_bin/Tags.ashx")
       ]
        public string TagPage
        {
            get { return tagJSONPage; }
            set { tagJSONPage = value; }
        }
    }
}
